﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PengLuaKit
{
    //
    // @MonoSingleton: Singleton for MonoBehaviour Object
    //
    public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T _instance;
        public static T sInstance
        {
            get { return _instance; }
        }

        protected void Awake()
        {
            if (_instance != null)
            {
                DestroyImmediate(gameObject);
                return;
            }

            _instance = gameObject.GetComponent<T>();
            InitOnAwake();
        }

        protected void Start()
        {
            InitOnStart();
        }

        protected void OnDestroy()
        {
            if (_instance == this)
            {
                _instance = null;
            }
            ReleaseOnDestroy();
        }

        // awake
        protected virtual void InitOnAwake() { }

        // start
        protected virtual void InitOnStart() { }

        // destroy
        protected virtual void ReleaseOnDestroy() { }
    }
}